﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using store.Areas.Home.Models;

namespace store.Areas.Home.Controllers
{
    public class CartController : Controller
    {
        // GET: /Home/Cart/
        public ActionResult Index()
        {
            var basketModel = new BasketModel();
            if (Session["Basket"] != null)
            {
                basketModel = (BasketModel)Session["Basket"];
            }
            return View(basketModel);
        }

        [ChildActionOnly]
        public ActionResult CartHeader()
        {
            var basketModel = new BasketModel();
            try
            {
                if (Session["Basket"] != null)
                {
                    basketModel = (BasketModel)Session["Basket"];
                }
            }
            catch (Exception)
            {
            }
            return PartialView("_PartialBasket", basketModel);
        }

        [HttpPost]
        public ActionResult AddToCart(string productId, string name, string images, string qty, string price)
        {
            var listsimg = images.Split(',');
            var item = new Item()
                {
                    CartId = Guid.NewGuid().ToString(),
                    ProductId = int.Parse(productId),
                    name = name,
                    images = listsimg[1],
                    Price = decimal.Parse(price),
                    Count = 1,
                    DateCreated = DateTime.Now,
                };
            var basketModel = new BasketModel();
            var list = new List<Item>();
            decimal totalPrice;
            //neu gio hang da co san
            if (Session["Basket"] != null)
            {
                basketModel = (BasketModel)Session["Basket"];
                //tinh tong gia tien + them hang moi
                foreach (var itemOld in basketModel.lists.Where(itemOld => itemOld.ProductId == int.Parse(productId)))
                {
                    basketModel.TotalPrice = basketModel.TotalPrice - (itemOld.Price * itemOld.Count);
                    basketModel.lists.Remove(itemOld);
                    item.Count = itemOld.Count + item.Count;
                    break;
                }
                list = basketModel.lists;
                list.Add(item);
                totalPrice = basketModel.TotalPrice + (item.Price * item.Count);
            }
            else
            {
                // gio hang khong co san thi tao moi
                list.Add(item);
                totalPrice = item.Price * item.Count;
            }
            basketModel.lists = list;
            basketModel.TotalPrice = totalPrice;
            Session["Basket"] = basketModel;
            return PartialView("_PartialBasket", basketModel);
        }

        [HttpPost]
        public ActionResult RemoveFromCart(string id)
        {
            var basketModel = new BasketModel();
            if (Session["Basket"] != null)
            {
                basketModel = (BasketModel)Session["Basket"];
                foreach (var itemOld in basketModel.lists.Where(itemOld => itemOld.ProductId == int.Parse(id)))
                {
                    basketModel.lists.Remove(itemOld);
                    basketModel.TotalPrice = basketModel.TotalPrice - (itemOld.Count * itemOld.Price);
                    break;
                }
                Session["Basket"] = basketModel;
            }
            return PartialView("_PartialBasket", basketModel);
        }
    }
}
